package objects;

import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.PointLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import start.Config;

/**
 * Konfiguration der Szenenbeleutung
 *
 * @version 1.0
 * @author Imanuel Rohlfing
 * @author Dennis Wittkoetter
 */
public class Lights {
    /**
     * BranchGroup fuer die Lichter
     */
    private BranchGroup lightBG;

    /**
     * Konstruiert die Lichtobjekte, legt deren Eigenschaften fest, platziert
     * sie im Raum und fuegt sie der BranchGroup hinzu.
     */
    public Lights() {
        lightBG = new BranchGroup();

        Point3d lightPosition;
        int lightLength;
        Color3f lightColour;
        Vector3f lightDirection;

        //Licht links oben
        lightPosition = new Point3d(-0.1, 1.0, -1.95);
        lightDirection = new Vector3f(0.1f, -1.0f, 1.95f);
        lightLength = 2;
        lightColour = new Color3f(0.3f, 0.3f, 0.3f);

        DirectionalLight light01 = new DirectionalLight(lightColour, lightDirection);
        BoundingSphere bounds01 = new BoundingSphere(lightPosition, lightLength);
        light01.setInfluencingBounds(bounds01);
        lightBG.addChild(light01);
        if (Config.debug)
        	paintCube(lightPosition);

        //Licht rechts oben
        lightPosition = new Point3d(-0.1, 1.0, 1.95);
        lightDirection = new Vector3f(0.1f, -1.0f, -1.95f);
        lightLength = 2;
        lightColour = new Color3f(0.3f, 0.3f, 0.3f);

        DirectionalLight light02 = new DirectionalLight(lightColour, lightDirection);
        BoundingSphere bounds02 = new BoundingSphere(lightPosition, lightLength);
        light02.setInfluencingBounds(bounds02);
        lightBG.addChild(light02);
        if (Config.debug)
        	paintCube(lightPosition);

        //Licht hinten
        lightPosition = new Point3d(2.0, 1.0, 0.0);
        lightDirection = new Vector3f(-2.0f, -1.0f, 0.0f);
        lightLength = 3;
        lightColour = new Color3f(0.5f, 0.5f, 0.5f);

        DirectionalLight light03 = new DirectionalLight(lightColour, lightDirection);
        BoundingSphere bounds03 = new BoundingSphere(lightPosition, lightLength);
        light03.setInfluencingBounds(bounds03);
        lightBG.addChild(light03);
        if (Config.debug)
        	paintCube(lightPosition);

        //Licht vorne
        lightPosition = new Point3d(-1.5, 0.5, 0.0);
        lightLength = 3;
        lightColour = new Color3f(1.0f, 1.0f, 1.0f);
        PointLight light04 = new PointLight(
                lightColour,
                new Point3f((float) lightPosition.x, (float) lightPosition.y, (float) lightPosition.z),
                new Point3f(0.0f, 1.0f, 0.0f));

        BoundingSphere bounds04 = new BoundingSphere(lightPosition, lightLength);
        light04.setInfluencingBounds(bounds04);
        lightBG.addChild(light04);
        if (Config.debug)
        	paintCube(lightPosition);
    }

    /**
     * Liefert Branchgroup der Lichtobjekte.
     *
     * @return
     */
    public BranchGroup get() {
        return lightBG;
    }

    /**
     * Zeichnet farbigen Ball um die Lichtobjekte zum Debugging.
     *
     * @param point	Positionskoordinaten des Lichtobjekts
     */
    private void paintCube(Point3d point) {
        if (Config.debug) {
            Transform3D translate = new Transform3D();
            translate.set(new Vector3d(point.x, point.y, point.z));
            TransformGroup tGroup = new TransformGroup();
            tGroup.setTransform(translate);
            tGroup.addChild(new ColorCube(0.07));
            this.lightBG.addChild(tGroup);
        }

    }
}
